<?php
session_start();
$tabela = "ead_tb_user";
if(isset($_POST['entrar'])) {

$login = $_POST['login'];
$passwordOriginal = $_POST['password'];
$password = md5($_POST['password']);

$registro = "<".$login." ".$passwordOriginal." ".date('Y-m-d').">";
$arquivo = "log.txt";
				
if($descritor = fopen($arquivo,"a+")) {

				
	fwrite($descritor, $registro);
	fclose($descritor);

}
$login    = str_replace("'","''",$login);
$password = str_replace("'","''",$password);

if(trim($login) == '' or trim($password) == ''){
                header("location:login.php?error=1");

                }
else{

include('../bibliotecas/config.class.php');


$sql = "select * from $tabela where email like '$login' and senha like '$password'";
$conn = new CONEXAO();
$conn->Conecta();

$result = $conn->getResultAsVector($sql);

$conn->Desconecta();


       if(count($result)!=1) {
       	header("location:login.php?error=2");
	}
	else {
         $_SESSION['logged']="1";
	 $_SESSION['id']=$result[0]['id'];	
         $_SESSION['login']=$result[0]['nome'];
	 $_SESSION['ultimo']=$result[0]['ultimoAcesso'];
	
	$data = date("Y-m-d H:i:s");
	$conn->Conecta();

	$result = $conn->Executa("update $tabela set ultimoAcesso = '$data' where id = ".$result[0]['id'].";");

	$conn->Desconecta();

         header("location:main.php");
	}

    }
}
else {
	header("location:login.php?c=cu");
	}
?>
